#ifndef __CLI_PARSE__EFFECT_H__
#define __CLI_PARSE__EFFECT_H__

#include "structs.h"
#include "resource.h" // Error Messages

#define GAME_ERROR(X) GameError(X)

class eFile;

namespace Underlight
{
  struct Sound
  {
    unsigned char* data;
    unsigned short sample_rate;
    unsigned char  bits_per_sample;
    unsigned char  num_channels;
  };

  struct Palette
  {
    unsigned char color [PALETTE_SIZE];
  };

  /**
   * Manages all of the Underlight visual / sound effects required
   *  by cli_parse.
   *
   *  * Use Underlight::effects-> to access the Effect Manager.
   *
  **/
  class EffectManager
  {
  public:
     EffectManager (void) {
       file = NULL;

       palette_headers       = NULL;
       visual_effect_headers = NULL;
       sound_effect_headers  = NULL;

       num_visual_effects  = num_palettes = 
       num_active_palettes = num_sound_effects = 0;

       visual_effect_bytes = 0;
       sound_effect_bytes  = 0;
     }

    ~EffectManager (void);

    bool LoadEffectFile (const TCHAR* filename);

    void LoadPermanentVisualEffects (void);
    void LoadPermanentPalettes      (void);
    void LoadPermanentSoundEffects  (void);

    int  LoadEffectBitmaps (id_t effect_id);
    void FreeEffectBitmaps (id_t effect_id);

    int  LoadSkyBitmap     (id_t effect_id);

    int  LoadEffectPalette (id_t palette_id);
    void FreeEffectPalette (id_t palette_id);

    int  LoadSoundEffect   (id_t sound_id);
    void FreeSoundEffect   (id_t sound_id);

    void LoadSound         (int index, id_t sound_id);


    // Selectors
    int EffectFrames  (id_t effect_id);
    int EffectViews   (id_t effect_id);
    int EffectHeight  (id_t effect_id);
    int EffectWidth   (id_t effect_id);
    int EffectPalette (id_t effect_id);

    int VEBytes (void) { return visual_effect_bytes; };
    int SEBytes (void) { return sound_effect_bytes;  };

    VisualEffectHeader* GetBitmapHeader  (id_t id);
    BITMAPINFO_4DX*     GetBitmap        (id_t id, int frame = 0);

    PaletteHeader*      GetPaletteHeader (id_t id);
    Palette*            GetPalette       (id_t id);

    SoundEffectHeader*  GetSoundHeader   (id_t id);
    Sound*              GetSound         (id_t id);


  private:
    bool LoadVisualEffect (id_t effect_id, bool sky);
    void LoadPalette      (id_t effect_id);


  protected:
    eFile* file; // Keep the file open, we will read from it as needed.

  private:
    BITMAPINFO_4DX bitmap_info [MAX_BITMAPS]; // Effects bitmap data
    Palette        palettes    [MAX_PALETTES];
    Sound          sounds      [MAX_SOUNDS];

    PaletteHeader*      palette_headers;
    VisualEffectHeader* visual_effect_headers;
    SoundEffectHeader*  sound_effect_headers;

    int num_visual_effects;
    int num_palettes;
    int num_active_palettes;
    int num_sound_effects;

    int visual_effect_bytes;
    int sound_effect_bytes;
  } extern *effects;
}

#endif /* __CLI_PARSE__EFFECT_H__ */
